
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="用户Id:" prop="userId">
    <el-input v-model.number="formData.userId" :clearable="true" placeholder="请输入用户Id" />
</el-form-item>
        <el-form-item label="商品Id:" prop="shopId">
    <el-input v-model.number="formData.shopId" :clearable="true" placeholder="请输入商品Id" />
</el-form-item>
        <el-form-item label="订单类型:" prop="orderType">
    <el-input v-model="formData.orderType" :clearable="true" placeholder="请输入订单类型" />
</el-form-item>
        <el-form-item label="订单金额:" prop="orderAmount">
    <el-input v-model.number="formData.orderAmount" :clearable="true" placeholder="请输入订单金额" />
</el-form-item>
        <el-form-item label="订单状态（字典）:" prop="orderStatus">
    <el-select v-model="formData.orderStatus" placeholder="请选择订单状态（字典）" style="width:100%" filterable :clearable="true">
        <el-option v-for="(item,key) in order_statusOptions" :key="key" :label="item.label" :value="item.value" />
    </el-select>
</el-form-item>
        <el-form-item label="创建时间:" prop="orderCreateTime">
    <el-date-picker v-model="formData.orderCreateTime" type="date" style="width:100%" placeholder="选择日期" :clearable="true" />
</el-form-item>
        <el-form-item label="支付时间:" prop="orderPayTime">
    <el-date-picker v-model="formData.orderPayTime" type="date" style="width:100%" placeholder="选择日期" :clearable="true" />
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createLsOrder,
  updateLsOrder,
  findLsOrder
} from '@/api/shop/lsOrder'

defineOptions({
    name: 'LsOrderForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const order_statusOptions = ref([])
const formData = ref({
            userId: undefined,
            shopId: undefined,
            orderType: '',
            orderAmount: undefined,
            orderStatus: '',
            orderCreateTime: new Date(),
            orderPayTime: new Date(),
        })
// 验证规则
const rule = reactive({
               userId : [{
                   required: true,
                   message: '请输入用户Id',
                   trigger: ['input','blur'],
               }],
               shopId : [{
                   required: true,
                   message: '请选择商品',
                   trigger: ['input','blur'],
               }],
               orderStatus : [{
                   required: true,
                   message: '请选择订单状态',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findLsOrder({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    order_statusOptions.value = await getDictFunc('order_status')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createLsOrder(formData.value)
               break
             case 'update':
               res = await updateLsOrder(formData.value)
               break
             default:
               res = await createLsOrder(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
